package com.mythesis.bpel.ui.client.wrapper;


import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyDownEvent;
import com.google.gwt.event.dom.client.KeyDownHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.user.client.Window;
import com.google.gwt.widgetideas.graphics.client.Color;
import com.google.gwt.widgetideas.graphics.client.GWTCanvas;
import com.mythesis.bpel.ui.client.controls.BpelControl;

public class ComponentControlWrapper extends ControlWrapper{

	public ComponentControlWrapper(BpelControl control) {
		super(control);
		// TODO Auto-generated constructor stub
	}

	@Override
	public void addControl(ControlWrapper wrapper) {
		// TODO Auto-generated method stub
	}

	@Override
	public void removeControl(ControlWrapper wrapper) {
		// TODO Auto-generated method stub
	}

	@Override
	public void renderControl() {
		// TODO Auto-generated method stub
		calculateOutline();
		
		Boundary a = controlOutline;
		outlinePanel.setPixelSize(a.w, a.h);
		canvas.resize(a.w, a.h);
		canvas.setLineWidth(1);
		canvas.setStrokeStyle(Color.GREY);
		canvas.setLineJoin(GWTCanvas.ROUND);
		canvas.setLineCap(GWTCanvas.ROUND);
		canvas.beginPath();
		canvas.moveTo(a.x, a.y);
		canvas.lineTo(a.x + a.w, a.y);
		canvas.lineTo(a.x + a.w, a.y + a.h);
		canvas.lineTo(a.x, a.y + a.h);
		canvas.closePath();
		canvas.stroke();

		outlinePanel.add(canvas, 0, 0);
		outlinePanel.add(this.getControl(), 5, 5);
		setWidget(outlinePanel);
	}

	@Override
	public Boundary getOuline() {
		// TODO Auto-generated method stub
		return controlOutline;
	}

	@Override
	public void calculateOutline() {
		// TODO Auto-generated method stub
		int h = this.getControl().getOffsetHeight();
		int w = this.getControl().getOffsetWidth();
		controlOutline.setX(0);
		controlOutline.setY(0);
		controlOutline.setWidth(w+10);
		controlOutline.setHeight(h+10);
	}

	@Override
	public void renderFocus() {
		// TODO Auto-generated method stub
		if(!focus){
			renderControl();
			Boundary r = getOuline();
			canvas.setLineWidth(1);
			canvas.setFillStyle(Color.BLACK);
			canvas.fillRect(r.x, r.y, 4, 4);
			canvas.fillRect(r.x, r.y + r.h-4, 4, 4);
			canvas.fillRect(r.x + r.w -4, r.y + r.h -4, 4, 4);
			canvas.fillRect(r.x + r.w -4, r.y, 4, 4);
		}
		else {
			canvas.clear();
			renderControl();
		}
		focus = !focus;
	}
	
}
